package com.bonc.ioc.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 判断 两个时间 是否是 同一时间
 * @author ZhangXu
 * @date 2021/4/19 11:52
 */
public class SameTimeUtil {

    public final static String DAY = "day";
    public final static String MONTH = "month";
    public final static String YEAR = "year";
    public final static String HOUR = "hour";
    public final static String MINUTE = "minute";
    public final static String SECOND = "second";

    /**
     * 判断 两个时间 是否是 同一时间
     * @param date1 时间1
     * @param date2 时间2
     * @param type 时间单位 DAY; MONTH; YEAR
     * @return
     */
    public static boolean isSameTime(Date date1, Date date2, String type) {
        if(date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            switch (type) {
                case "day" :
                    return isSameDay(cal1, cal2);
                case "month" :
                    return isSameMonth(cal1, cal2);
                case "year" :
                    return isSameYear(cal1, cal2);
                case "hour" :
                    return isSameHour(cal1, cal2);
                case "minute" :
                    return isSameMinute(cal1, cal2);
                case "second" :
                    return isSameSecond(cal1, cal2);
                default:
                    throw new IllegalArgumentException("The type must be valid");
            }
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    private static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    private static boolean isSameMonth(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(2) == cal2.get(2);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    private static boolean isSameYear(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    private static boolean isSameHour(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6) && cal1.get(11) == cal2.get(11);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    private static boolean isSameMinute(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6) && cal1.get(11) == cal2.get(11) && cal1.get(12) == cal2.get(12);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    private static boolean isSameSecond(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6) && cal1.get(11) == cal2.get(11) && cal1.get(12) == cal2.get(12) && cal1.get(13) == cal2.get(13);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }
}
